package com.cskaoyan.service.wx;

import com.cskaoyan.bean.Collect;
import com.cskaoyan.bean.CollectExample;
import com.cskaoyan.bean.wx.CollectList;
import com.cskaoyan.bean.wx.WxCollectVO;
import com.cskaoyan.mapper.CollectMapper;
import com.cskaoyan.utils.IdUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author WeitenHong
 * @date 2020/10/6 20:23
 */
@Service
public class WxCollectServiceImpl implements WxCollectService{

    @Autowired
    CollectMapper collectMapper;

    @Override
    public WxCollectVO list(Integer type, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<CollectList> collectList=collectMapper.queryCollects(IdUtils.getId(),type);
        PageInfo<CollectList> pageInfo = new PageInfo<>(collectList);
        WxCollectVO wxCollectVO = new WxCollectVO();
        wxCollectVO.setCollectList(collectList);
        wxCollectVO.setTotalPages(pageInfo.getPages());
        return wxCollectVO;
    }

    @Override
    public String addOrDelete(Integer userId, Byte type, Integer valueId) {
        CollectExample collectExample = new CollectExample();
        collectExample.createCriteria().andUserIdEqualTo(userId).andTypeEqualTo(type)
                .andValueIdEqualTo(valueId);
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        if(collects.size()==0){
            Collect collect = new Collect();
            collect.setUserId(userId);
            collect.setType(type);
            collect.setValueId(valueId);
            collect.setAddTime(new Date());
            collect.setUpdateTime(new Date());
            collectMapper.insertSelective(collect);
            return "add";
        }
        collectMapper.deleteByExample(collectExample);
        return "delete";
    }
}
